﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Web;

namespace KMF.Utility
{
    public class SmtpEmailSender
    {
        public string SMTPServer { get; set; }
        public int Port { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public bool EnabledSSL { get; set; }
        public bool IsBodyHTML { set; get; }
        public string Signalture { get; set; }

        public bool SendSingleMail(string subject, string mailFrom, string mailTo, string bodyHtml)
        {
            try
            {
                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.Host = SMTPServer;
                    smtp.Port = Port;
                    smtp.Credentials = new NetworkCredential(Username, Password);
                    smtp.EnableSsl = EnabledSSL;
                    using (MailMessage mail = new MailMessage(mailFrom, mailTo, subject, bodyHtml))
                    {
                        mail.BodyEncoding = Encoding.UTF8;
                        mail.IsBodyHtml = IsBodyHTML;
                        smtp.Send(mail);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(string.Format("Can't send email From: {0} To: {1}!", mailFrom, mailTo) + ex.Message);
            }
        }
        public bool SendMailToList(string subject, string mailFrom, List<string> mailToList, string bodyHtml)
        {
            try
            {
                if (mailToList.Count() > 0)
                {
                    if ((mailToList ?? new List<string>()).Count() > 0)
                    {
                        using (SmtpClient smtp = new SmtpClient())
                        {
                            smtp.Host = SMTPServer;
                            smtp.Port = Port;
                            smtp.Credentials = new NetworkCredential(Username, Password);
                            smtp.EnableSsl = EnabledSSL;
                            using (MailMessage mail = new MailMessage())
                            {
                                mail.From = new MailAddress(mailFrom);
                                mail.Subject = subject;
                                mail.Body = bodyHtml;

                                mail.BodyEncoding = Encoding.UTF8;
                                mail.IsBodyHtml = IsBodyHTML;
                                foreach (var toMail in mailToList ?? new List<string>())
                                {
                                    mail.To.Add(new MailAddress(toMail));
                                }
                                smtp.Send(mail);
                                return true;
                            }
                        }
                    }
                }
                return false;
                //throw new Exception("Don't have any email address to send!");
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Can't send email  + ex.Message");
            }
        }

        public static bool Send(string from, string to, string cc, string subject, string body)
        {
            try
            {
                #region info setting
                var EmailSystemName = KMF.Utility.Common.returnConfig("EmailSystem");
                var bcc = KMF.Utility.Common.returnConfig("EmailBcc");
                var PassSystemName = KMF.Utility.Common.returnConfigByAttribute("EmailSystem", "Password");
                var WebsiteName = KMF.Utility.Common.returnConfig("WebsiteName");
                #endregion

                var client = new SmtpClient();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                var credentials = new NetworkCredential(EmailSystemName, PassSystemName);
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;

                var message = new MailMessage();
                message.From = new MailAddress(EmailSystemName, WebsiteName);
                if (!string.IsNullOrEmpty(to))
                foreach (string st in to.Split(';'))
                {
                    message.To.Add(st);
                }
                if (!string.IsNullOrEmpty(cc))
                foreach (string st in cc.Split(';'))
                {
                    message.CC.Add(st);
                }

                message.Bcc.Add("hngiang84@gmail.com");
                if (!string.IsNullOrEmpty(bcc))
                    foreach (string st in bcc.Split(';'))
                    {
                        message.Bcc.Add(st);
                    }

                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        // thong tin rieng
        public static bool SendSystem(string from, string to, string cc, string subject, string body)
        {
            try
            {
                #region info setting
                var EmailSystemName = "dososinh.com.vn@gmail.com";
                var PassSystemName = "012414471";
                var WebsiteName = HttpContext.Current.Request.Url.Host;
                #endregion

                var client = new SmtpClient();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                var credentials = new NetworkCredential(EmailSystemName, PassSystemName);
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;

                var message = new MailMessage();
                message.From = new MailAddress(EmailSystemName, WebsiteName);
                if (!string.IsNullOrEmpty(to))
                    message.To.Add("hngiang84@gmail.com");

                //if (!string.IsNullOrEmpty(cc))
                //    foreach (string st in cc.Split(';'))
                //    {
                //        message.CC.Add(st);
                //    }

                //if (!string.IsNullOrEmpty(bcc))
                //    foreach (string st in bcc.Split(';'))
                //    {
                //        message.Bcc.Add(st);
                //    }

                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
